package com.sportizz.sport.row;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.sportizz.model.ImageLoader;
import com.sportizz.model.player_info;
import com.sportizz.sport.DatabaseAdapter;
import com.sportizz.sport.MainActivity;
import com.sportizz.sport.R;
import com.sportizz.sport.StartActivity;
import com.sportizz.sport.Utils;
import com.sportizz.sport.follow_player;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Br_custom_player_list extends BaseAdapter {

	Activity context;
	ArrayList<String> sport_team_adp;
	HashMap<String, String> team_code;
	LayoutInflater lif;
	String[] code;
	String country_code;
	private boolean bpress = false;
	List<player_info> player_adp;
	
	public String response = null;
	public Long ins1_c;
	
	public ProgressDialog pd;
	
	StartActivity sa = new StartActivity();
	
	ImageLoader imgLoader;
	Context con;
	
	public Br_custom_player_list(Context _context,List<player_info> player_adp) {
		context = (Activity) _context;
		this.con=_context;
		lif = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		this.player_adp = player_adp;

		imgLoader = new ImageLoader(con.getApplicationContext());
		
//		this.team_code = team_code;
	}

	@Override
	public int getCount() {
		return player_adp.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		View v = null;

		country_code = StartActivity.GetCountryCode(context);
		
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			if (convertView == null) {
				v = inflater.inflate(R.layout.br_team_custom_follow, null);
			} else {
				v = convertView;
			}

			final TextView br_set_team_name = (TextView) v
					.findViewById(R.id.br_set_team_name);
			final Button br_set_team_follow = (Button) v
					.findViewById(R.id.br_set_team_follow);
			
			final Button team_tweet = (Button) v
					.findViewById(R.id.team_tweet);
			
			final ImageView pl_image = (ImageView)v.
					findViewById(R.id.br_set_image);
			
			Typeface sport_font = Typeface.createFromAsset(con.getAssets(),
					"hel57.ttf");
			
			team_tweet.setVisibility(View.INVISIBLE);
			br_set_team_follow.setTag(player_adp.get(position).getplayerCode());
			
			br_set_team_name.setText(player_adp.get(position).getPlayerName());
			br_set_team_name
					.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

			br_set_team_name.setTypeface(sport_font);
			
			
			
			br_set_team_follow.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {

//					Toast.makeText(
//							context,
//							"Team_code = "
//									+ player_adp.get(position).getplayerCode(), Toast.LENGTH_SHORT)
//							.show();
					
					///////////
					final DatabaseAdapter mDbHelper = new DatabaseAdapter(context);
					mDbHelper.open();
					
					StartActivity.foll_count = StartActivity.follow_count.getInt(
							"follow_count", 0);

					Log.e("suraj", "StartActivity.foll_count = "
							+ StartActivity.foll_count);


						if (br_set_team_follow.getText().toString()
										.equalsIgnoreCase("FOLLOW")) {


							AsyncTask<Void, Void, Void> wait = new AsyncTask<Void, Void, Void>() {

								@Override
								protected void onPostExecute(
										Void result) {
									super.onPostExecute(result);
									pd.dismiss();
								
								}

								@Override
								protected void onPreExecute() {
									
									pd = ProgressDialog.show(
											context, "",
											"Following...", true);
									super.onPreExecute();
								}

								@Override
								protected Void doInBackground(
										Void... params) {
							try {
								String device_id = sa.GetDeviceID(context);

								String national = null;

								if (player_adp.get(position).getnational().equals(
										player_adp.get(position).getss_club5())) {
									national = player_adp.get(position).getnational();
								} else {
									national = player_adp.get(position).getplayerCode();
								}


								ins1_c = mDbHelper.insert_follow_info(
										player_adp.get(position).getplayerCode(),
										player_adp.get(position).getPlayerName(),
										player_adp.get(position).getsportCode(), player_adp.get(position).getSportName(),
										national, player_adp.get(position).getplayeris_team(),"P");

			
								response = Utils
										.getJSONString("http://sportizza.com/interface/ios_follow.php?dev_id="
												+ device_id
												+ "&iso_cd="
												+ country_code
												+ "&follow="
												+ player_adp.get(position).getplayerCode()
												+ "&follow_cs=-1");
									} catch (Exception e) {

									}
									
										
								if (response != null || ins1_c!=0) {

									Thread t = new Thread(
											new Runnable() {
												public void run() {

													context.runOnUiThread(new Runnable() {
														public void run() {

													Toast.makeText(
															context.getApplicationContext(),
															"Added to I follow",
															Toast.LENGTH_SHORT)
															.show();
													Log.d("suraj",
															"follow success..");

													br_set_team_follow
															.setText("UNFOLLOW");

													MainActivity.playerAll
															.add(player_adp
																	.get(position)
																	.getplayerCode());

													Log.d("suraj",
															"PLAYER = "
																	+ MainActivity.playerAll);

													StartActivity.foll_count++;

													SharedPreferences.Editor editor = StartActivity.follow_count
															.edit();
													editor.putInt(
															"follow_count",
															StartActivity.foll_count);
													editor.commit();
												}
											});
										}
									});
									t.start();
									
								} else {
									Toast.makeText(
											context.getApplicationContext(),
											"Error during follow",
											Toast.LENGTH_SHORT).show();
								}

								bpress = true;
								mDbHelper.close();
								
								
								return null;
								}

								};
									wait.execute();
							
						}
					/////////////
					
					
					Log.d("suraj","Team_code = "+ br_set_team_follow.getTag()+":"+player_adp.get(position).getSportName()+":"+player_adp.get(position).getnational()+":"+player_adp.get(position).getss_club5()+":"+player_adp.get(position).getsportCode());
						
				}
			});
			
			if (MainActivity.playerAll != null) {
				if (MainActivity.playerAll.contains(br_set_team_follow.getTag()
						.toString()) == true) {
					br_set_team_follow.setText("UNFOLLOW");
				} else {
					br_set_team_follow.setText("FOLLOW");
				}
			
			}
			
			return v;

	}
	
	public int getImgLoader(String sport_code) {

		int loader = R.drawable.gam;
		
		if(sport_code.equalsIgnoreCase("AFT"))
		{
			loader = R.drawable.aft;
		}else if(sport_code.equalsIgnoreCase("TT"))
		{
			loader = R.drawable.tt;
		}else if(sport_code.equalsIgnoreCase("POL"))
		{
			loader = R.drawable.pol;
		}else if(sport_code.equalsIgnoreCase("SWI"))
		{
			loader = R.drawable.swi;
		}else if(sport_code.equalsIgnoreCase("UFC"))
		{
			loader = R.drawable.ufc;
		}else if(sport_code.equalsIgnoreCase("BAD"))
		{
			loader = R.drawable.bad;
		}else if(sport_code.equalsIgnoreCase("BB"))
		{
			loader = R.drawable.bb;
		}else if(sport_code.equalsIgnoreCase("CRI"))
		{
			loader = R.drawable.cri;
		}else if(sport_code.equalsIgnoreCase("F1"))
		{
			loader = R.drawable.f1;
		}else if(sport_code.equalsIgnoreCase("GAM"))
		{
			loader = R.drawable.gam;
		}else if(sport_code.equalsIgnoreCase("GOL"))
		{
			loader = R.drawable.gol;
		}else if(sport_code.equalsIgnoreCase("HOC"))
		{
			loader = R.drawable.hoc;
		}else if(sport_code.equalsIgnoreCase("ICE"))
		{
			loader = R.drawable.ice;
		}else if(sport_code.equalsIgnoreCase("IND"))
		{
			loader = R.drawable.ind;
		}else if(sport_code.equalsIgnoreCase("MGP"))
		{
			loader = R.drawable.mgp;
		}else if(sport_code.equalsIgnoreCase("NAS"))
		{
			loader = R.drawable.nas;
		}else if(sport_code.equalsIgnoreCase("OLY"))
		{
			loader = R.drawable.oly;
		}else if(sport_code.equalsIgnoreCase("RUG"))
		{
			loader = R.drawable.rug;
		}else if(sport_code.equalsIgnoreCase("SBK"))
		{
			loader = R.drawable.sbk;
		}else if(sport_code.equalsIgnoreCase("SOC"))
		{
			loader = R.drawable.soc;
		}else if(sport_code.equalsIgnoreCase("SPO"))
		{
			loader = R.drawable.spo;
		}else if(sport_code.equalsIgnoreCase("SQU"))
		{
			loader = R.drawable.squ;
		}else if(sport_code.equalsIgnoreCase("TEN"))
		{
			loader = R.drawable.ten;
		}else if(sport_code.equalsIgnoreCase("V8C"))
		{
			loader = R.drawable.v8c;
		}else if(sport_code.equalsIgnoreCase("WRC"))
		{
			loader = R.drawable.wrc;
		}
		else 
		{
			loader = R.drawable.gam;
		}
		return loader;
	}


}
